package sol.leetcode;

import java.util.*;

/**
 * Intersection of Two Arrays
 */
public class Issue349 {
    public static int[] intersection(int[] nums1, int[] nums2) {
        Set<Integer> set = new HashSet<Integer>();
        List<Integer> res = new ArrayList<Integer>();
        for (int num : nums1)
            set.add(num);
        for (int num : nums2)
            if (set.contains(num)) {
                res.add(num);
                set.remove(num);
            }

        int[] resArr = new int[res.size()];
        for (int i = 0; i < resArr.length; ++i)
            resArr[i] = res.get(i);

        return resArr;
    }

    public static void main(String[] args) {
        int[] nums1 = {1, 2, 2, 1};
        int[] nums2 = {2, 2};
        System.out.println(Arrays.toString(intersection(nums1,nums2)));
    }
}
